﻿#region Using's

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using MyUtils;
using Microsoft.VisualStudio.TestTools.UnitTesting;

#endregion

#pragma warning disable 612,618

namespace Test.MyUtils
{
	[TestClass]
	public class StringOperations
	{
		static void checkSizeConverter(long val, string expected)
		{
			checkSizeConverter(val, expected, false);
		}

		static void checkSizeConverter(long val, string expected, bool oneWay)
		{
			Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
			Assert.AreEqual(expected, val.ToSizeString(), val.ToString());
			if (!oneWay)
			{
				Assert.AreEqual(val, Utils.ParseSizeString(expected), expected + " - "+Utils.ParseSizeString(expected));
			}
		}

		[TestMethod]
		public void Should_allow_parametrize_strings()
		{
			var actual = "this {0} {1:0.00} funny{2}".Arg("is", Math.PI, " day");
			Assert.IsTrue(Regex.IsMatch(actual, "this is 3[,.]14 funny day"));
		}

		[TestMethod]
		public void Should_convert_size_to_useful_string()
		{
			checkSizeConverter(0, "0 B");
			checkSizeConverter(32, "32 B");
			checkSizeConverter(1023, "1023 B");
			checkSizeConverter(1024, "1 KB");
			checkSizeConverter(1025, "1 KB", true);
			checkSizeConverter(1126, "1.1 KB");
			checkSizeConverter(1536, "1.5 KB");
			checkSizeConverter(2048, "2 KB");
			checkSizeConverter(2049, "2 KB", true);
			checkSizeConverter(2150, "2.1 KB");
			checkSizeConverter(1024 * 1023 + 900, "1023.9 KB", true);
			checkSizeConverter(10485760, "10 MB");
			checkSizeConverter(1024 * 1024, "1 MB");
			checkSizeConverter(1024 * 1024 * 1024, "1 GB");
			checkSizeConverter(1024L * 1024 * 1024 * 1024, "1 TB");

			Assert.AreEqual("8 EB", long.MaxValue.ToSizeString());
			Assert.AreEqual("-8 EB", (long.MinValue).ToSizeString());
			Assert.AreEqual("-35 KB", (-1024L * 35).ToSizeString());
//			Assert.AreEqual("16 EB", ulong.MaxValue.ToSizeString());

// 			Assert.AreEqual("64 *2^90 B", decimal.MaxValue.ToSizeString());
			//Assert.AreEqual("-64 *2^90 B", decimal.MinValue.ToSizeString());
		}

		[TestMethod]
		public void Should_join_strings()
		{
			var strs = new[]
			{
				"abc", "def", "ghi"
			} as IEnumerable<string>;
			Assert.AreEqual("abc, def, ghi", strs.Join(", "));
		}

		[TestMethod]
		public void Should_split_string_to_lines()
		{
			var result = @"line0
line1
line2".SplitToLines();
			Assert.AreEqual(3, result.Length, "3 lines expected!");
			Assert.AreEqual("line0", result[0]);
			Assert.AreEqual("line1", result[1]);
			Assert.AreEqual("line2", result[2]);

			Assert.IsFalse("asddasd\r\nasdasdas\rasdasdasd\nasdasd\n\rasdasd".SplitToLines().Any(x => x.Contains('\r')));
			Assert.IsFalse("asddasd\r\nasdasdas\rasdasdasd\nasdasd\n\rasdasd".SplitToLines().Any(x => x.Contains('\n')));
		}
	}
}